<?php

namespace App\Api\V1\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Api\V1\Requests\ImageRequest;
use App\Functions\UploadFile;
use Tymon\JWTAuth\Exceptions\JWTException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ImageController extends Controller
{
    //
    /**
    * function_description.
    *
    * @param int|null post
    * @return string
    * @throws NotFoundHttpException
    */
    public function image(ImageRequest $request)
    {
            $file =$request->file('file');
            
            $path = UploadFile::save($file); 

            if(!$path){
                throw new HttpException(500);
            }

            return response()
            ->json([
                'status' => true,
                'path' => $path ,
                'base_url' => $_SERVER['HTTP_HOST']
            ]);

    }
    //h后台用
    public function image1(ImageRequest $request)
    {
            
            $file = $request->all('file');

            $path = UploadFile::save($file);

            if(!$path){
                throw new HttpException(500);
            }

            return response()
            ->json(
                [ 'files' =>[
                    [
                        'status' => true,
                        'path' => $path ,
                        'base_url' => $_SERVER['HTTP_HOST']
                    ]
                ]
            ]);
            
        
    }

   
}
